package py.edu.ucom.cadira.net.serializables;

import org.jdom.Document;
import org.jdom.Element;


import py.edu.ucom.cadira.game.war.WarUser;
import py.edu.ucom.cadira.net.User;
import py.edu.ucom.cadira.net.XMLSerializable;

public class JoinRequest implements XMLSerializable {
	
	private Integer tableroId;
	private static final String NODE_JOIN_TABLE = "joinTable";
	private Integer type;
	
	
	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public JoinRequest(Integer tableroId, Integer type) {
		super();
		this.tableroId = tableroId;
		this.type = type;
	}

	public JoinRequest() {
        super();
	}
	public JoinRequest(Integer tableroId) {
		super();
		this.tableroId = tableroId;
	}
	public Integer getTableroId() {
		return tableroId;
	}

	public void setTableroId(Integer tableId) {
		this.tableroId = tableId;
	}

	public JoinRequest(User usuario, Integer tableroId, String userType) {
		super();
		//this.usuario = usuario;
		this.tableroId = tableroId;
	}
	
	public void fromXML(Document xml) {
		Element root = xml.getRootElement();
		Element joinRequestNode = root.getChild(NODE_JOIN_TABLE);
		setTableroId(Integer.parseInt(joinRequestNode.getAttributeValue("tableId")));
		setType(getTypeInteger(joinRequestNode.getAttributeValue("type")));
	}
	public void toXML(Element root) {
		Element joinRequestNode = new Element(NODE_JOIN_TABLE);
		joinRequestNode.setAttribute("tableId",getTableroId().toString());
		joinRequestNode.setAttribute("type",getTypeString(this.type));
		root.addContent(joinRequestNode);
		
	}
	private String getTypeString(Integer type){
	    if(type == WarUser.PLAYER){
	        return "player";
	    }else{
	        return "watcher";
	    }
	}
    private Integer getTypeInteger(String type){
        if(type.compareTo("player") == 0){
            return WarUser.PLAYER;
        }else{
            return WarUser.WATCHER;
        }
    }
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof JoinRequest)) {
			return false;
		}
		JoinRequest otherObj = (JoinRequest) obj;
		if (otherObj.getType().equals(getType())) {
			return true;
		}
		return false;
	}
}